package rou.controller;

import org.springframework.beans.SimpleTypeConverter;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import rou.util.MethodUtil;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ervin on 2017/8/10.
 */
@Controller
public class RouteController {

    @Autowired
    ApplicationContext applicationContext;

    @RequestMapping( "/welcome/hello" )
    @ResponseBody
    public String hello(){
        return "hello,Snow";
    }

    @RequestMapping( "/route" )
    @ResponseBody
    public Object route(@RequestParam String serviceName, @RequestParam String serviceMethodName, HttpServletRequest request ){
        String[] parameterNames = null;
        Class<?>[] parameterTypes = null;
        Method proxyMethod = null;
        Object proxy = null;
        Object result = null;
        try{
            //服务id
            if(StringUtils.isEmpty( serviceName )){
                throw new Exception( "服务id缺失" );
            }
            //服务调用方法
            if(StringUtils.isEmpty( serviceMethodName )){
                throw new Exception( "服务方法缺失" );
            }
            try{
                proxy = applicationContext.getBean(serviceName);
            }catch ( Exception e ){
                throw new Exception( "服务id对应服务不存在" );
            }

            Method[] methods = proxy.getClass().getDeclaredMethods();
            Class<?>[] clazzes = proxy.getClass().getInterfaces();

            for (int i = 0; i < methods.length; i++) {
                Method method = methods[i];
                if (serviceMethodName.equals(method.getName())) {
                    proxyMethod = method;
                    break;
                }
            }

            if( proxyMethod == null ){
                throw new Exception( "服务中不存在服务方法" );
            }

            for (int i = 0; i < clazzes.length; i++) {
                Class<?> clazz = clazzes[i];
                try{
                    if (applicationContext.getBean(serviceName,clazz) != null) {
                        proxyMethod = clazz.getMethod(serviceMethodName, proxyMethod.getParameterTypes());
                        parameterNames = MethodUtil.getMethodParameterNamesByAnnotation( proxyMethod );
                        parameterTypes = proxyMethod.getParameterTypes();
                        break;
                    }
                }catch ( Exception e ){
                    ;
                }
            }

            List<Object> args = new ArrayList<Object>();
            for( int i = 0; i < parameterNames.length; i++ ){
                String parameterName = parameterNames[i];
                Class<?> parameterType = parameterTypes[i];
                String[] parameterValue = request.getParameterValues( parameterName );
                try{
                    TypeConverter converter = new SimpleTypeConverter();
                    Object paramValue = converter.convertIfNecessary( parameterValue !=null && parameterValue.length == 1 ? parameterValue[0] : parameterValue, parameterType );
                    args.add( paramValue );
                }catch ( Exception e ){
                    throw new Exception( "参数类型跟服务方法参数胡不匹配" );
                }
            }

            //服务调用
            result = proxyMethod.invoke( proxy, args.toArray() );

        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
